<?php
	function obtenerEstadoComplainTexto($estado){
		switch($estado){
			case 0: return "New"; break;
			case 1: return "Open"; break;
			case 2: return "Solved"; break;
			case 3: return "Archived"; break;
		}
	}
	
	function mostrarComplaints($id, $ok = NULL, $error = NULL){
		$admin = isAdmin();
		$q = "SELECT * FROM ((SELECT	c.id as id, 
								u.usuario as usuario, 
								u.id as usuario_id, 
								u.jugador as jugador, 
								c.estado as estado, 
								c.fecha as fecha, 
								c.last_fecha as last_fecha, 
								c.padre as padre, 
								c.acusado as acusado,
								c.partido as partido,  
								c.texto as texto, 
								NULL as image, 
								NULL as thumb, 
								NULL as image_width, 
								NULL as image_height, 
								NULL as thumb_width, 
								NULL as thumb_height 
			FROM complaint_partido as c, usuarios as u WHERE (c.id='".$id."' OR c.padre='".$id."') AND u.id = c.usuario AND c.imagen = '0') 
			UNION (SELECT	c.id as id, 
								u.usuario as usuario, 
								u.id as usuario_id, 
								u.jugador as jugador, 
								c.estado as estado, 
								c.fecha as fecha, 
								c.last_fecha as last_fecha, 
								c.padre as padre, 
								c.acusado as acusado,
								c.partido as partido,  
								c.texto as texto, 
								i.picture_path as image, 
								i.thumbnail_path as thumb, 
								i.width as image_width, 
								i.height as image_height, 
								i.thumbnail_width as thumb_width, 
								i.thumbnail_height as thumb_height	
			FROM complaint_partido as c, usuarios as u, image as i WHERE (c.id='".$id."' OR c.padre='".$id."') AND u.id = c.usuario AND i.id = c.imagen)) 			
			AS Q ORDER BY padre ASC, fecha ASC";
		$query = new mysqlQuery("tournament", $q, "", "", "", "", "");
		$query->execute();
		if($query->getCount() > 0){
			?>
			<div id="left-tournament">	
			<h1>Complaints</h1>
			<div class="container-tournament">			
			<?php 
				if(!is_null($ok)){
					echo '<div id="ok">'.$ok.'</div>';
				}
				
				if(!is_null($error)){
					echo '<div id="error">'.$error.'</div>';
				}
			
				$number = 1;
				$usuarios = array();
				$data = NULL;
				while($row = $query->next()){
					if($number == 1){
						$data = $row;
					}
					$usuarios[$row["usuario_id"]] = $row["usuario"];
					mostrarComplain($row, $number, $admin);
					$number++;
				}
			?>
			</div>
			<?php 
			if($admin){
				?>
			<h1>Post an anwser</h1>
			<div class="container-tournament">
				<form action="index.php?s=complaints-management&a=2&id=<?php echo $id;?>" method="post" enctype="multipart/form-data">
				<div class="comment-form">
				<p>Upload a Picture (optional)</p>
				<input type="file" name="picture" size="30" /><br/>
				<input type="hidden" name="match" value="<?php echo $data["partido"];?>"/>
				<p>Link a video from Youtube, Vimeo, or others (optional)</p>
				<input type="text" name="video" size="30" maxlength="255" /><br/>
				<p>Your Anwser</p>
				<textarea cols="63" rows="6" name="complaint"></textarea><br/>
				<input type="submit" value="Send Complaint" class="boton"/>
				</div>				
				</form>				
			</div>
			<?php
			}else if(array_key_exists($_SESSION["user_id"], $usuarios) && $data["estado"] < 1){			
			?>
			<h1>Post an anwser</h1>
			<div class="container-tournament">
				<form action="index.php?s=complaint&action=post-complaint&id=<?php echo $id;?>" method="post" enctype="multipart/form-data">
				<div class="comment-form">
				<p>Upload a Picture (optional)</p>
				<input type="file" name="picture" size="30" /><br/>
				<input type="hidden" name="match" value="<?php echo $data["partido"];?>"/>
				<p>Link a video from Youtube, Vimeo, or others (optional)</p>
				<input type="text" name="video" size="30" maxlength="255" /><br/>
				<p>Your Anwser</p>
				<textarea cols="63" rows="6" name="complaint"></textarea><br/>
				<input type="submit" value="Send Complaint" class="boton"/>
				</div>				
				</form>				
			</div>
			<?php } ?>
			</div>
			<div id="right-tournament">
			<h1>More Information</h1>
			<div class="container-tournament">
			<p>Started by: <strong><?php echo $data["usuario"];?></strong><br/>
			Started in: <strong><?php echo date("d.m.Y h:m", $data["fecha"]);?></strong><br/>
			Last updated: <strong><?php echo date("d.m.Y h:m", $data["last_fecha"]);?></strong><br/>
			Denonced user: <strong><?php echo $data["acusado"];?></strong><br>
			Status: <strong><?php echo obtenerEstadoComplainTexto($data["estado"]);?></strong></p>
			</div>
			</div>
			<?php
		}else{
			echo '<div id="error">Any complain found</div>';
		}
	}
	
	function mostrarComplain($data, $number, $admin){
		?>
		<div class="comment">
			<?php 
				if($admin){
					echo '<a href="index.php?s=complaints-management&a=3&id='.$data["id"].'&father='.$data["padre"].'" class="delete">Delete This</a>';
				}
			?>			
			<a href="#<?php echo $number;?>" name="<?php echo $number;?>" class="number">#<?php echo $number;?></a>
			<div class="author"><?php echo $data["jugador"]." (".$data["usuario"].")";?></div>
			<div class="date"><?php echo date("d.m.Y h:m", $data["fecha"]);?></div>
			<div class="text"><?php echo nl2br(utf8_decode($data["texto"]));?></div>
			<?php 
				if(strlen($data["image"]) > 0){
					echo '<a href="'.$data["image"].'" class="image" target="_blank">Image Attached</a>
							  <a href="'.$data["image"].'"><img src="'.$data["thumb"].'" width="'.$data["thumb_width"].'" height="'.$data["thumb_height"].'" alt="Complaint Picture"/></a>';
				}
				if(strlen($data["video"]) > 0){
					echo '<a href="'.$data["video"].'" class="video" target="_blank">Video Attached</a>';
				}
			?>
		</div>
		<?php
	}
	
	function obtenerComplainsPartido($idPartido){
		$q = "SELECT	c.id as id, 
								u.usuario as usuario, 
								u.id as usuario_id, 
								u.jugador as jugador, 
								c.estado as estado, 
								c.fecha as fecha, 
								c.last_fecha as last_fecha, 
								c.padre as padre, 
								c.acusado as acusado,
								c.partido as partido,  
								c.texto as texto, 
								c.responses as responses  	
		FROM complaint_partido as c, usuarios as u WHERE c.partido = '".$idPartido."' AND c.padre='0' AND c.usuario = u.id ORDER BY fecha ASC";
		$query = new mysqlQuery("tournament", $q, "", "", "", "", "");		
		$query->execute();
		$count = $query->getCount();
		if($count > 0){
			if($count == 1)
				echo '<p><strong>'.$count.' complaint</strong> attached to this match:</p>';
			else
				echo '<p><strong>'.$count.' complaints</strong> attached to this match:</p>';
			while($row = $query->next()){
				echo '<p>Started by: <strong>'.$row["usuario"].'</strong><br/>
				Status: <strong>'.obtenerEstadoComplainTexto($row["estado"]).'</strong><br/>
				Messages: <strong>'.($row["responses"] + 1).'</strong><br/>
				<a href="index.php?s=complaint&id='.$row["id"].'">Open complaint</a></p>';
			}
		}
	}
?>